package com.kedacom.freedomLabel.view.fragment.setting;


import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hard.CardReadHelp;
import com.hard.ReturnValue;
import com.hard.ReturnValueCallback;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.FragmentIDWriteBlockBinding;
import com.kedacom.freedomLabel.viewmodel.IDWriteBlockViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.util.Utils;
import com.lzscale.scalelib.misclib.Misc;

import java.util.regex.Pattern;

import static com.hard.CardReadHelp.toUnicode;


@ContentView(R.layout.fragment_i_d_write_block)
@ViewModel(IDWriteBlockViewModel.class)
public class IDWriteBlockFragment extends ASettingFragment<FragmentIDWriteBlockBinding, IDWriteBlockViewModel> {


    private CardReadHelp mReadCardHelp;
    private byte[] writeData = new byte[16];
    private byte[] writeDataProduct = new byte[16];
    protected Misc mMisc;

    @SuppressLint("HandlerLeak")
    private Handler mScaleHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {

                if (msg.obj.toString().contains("&")) {
                    String[] CardNum = msg.obj.toString().split("&");
                    mBinding.showReadCard.setText(CardNum[0]);
                    mBinding.showReadCardContent.setText(CardNum[1]);

                } else {
                    showToast(msg.obj.toString(), TOAST_WARNING);
                }
            } else if (msg.what == 1) {
                mBinding.showReadCardId.setText(msg.obj.toString());
            }else  if (msg.what == 3) {

                if (msg.obj.toString().contains("&")) {
                    String[] CardNum = msg.obj.toString().split("&");
                    mBinding.showReadCardProduct.setText(CardNum[0]);
                    mBinding.showReadCardProductContent.setText(CardNum[1]+" "+CardNum[2]);

                } else {
                    showToast(msg.obj.toString(), TOAST_WARNING);
                }
            }
        }
    };

    @Override
    public void saveData() {

    }

    @Override
    public String getFragmentTitle() {
        return Utils.getApp().getString(R.string.setting_frag_name_writelock);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReadCardHelp = CardReadHelp.getSingleton(); //new CardReadHelp();
        mMisc = Misc.newInstance();
    }

    @Override
    public void onStop() {
        super.onStop();
//        if (mReadCardHelp != null){
//            mReadCardHelp.cancelReadCard(null);
//        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReadCardHelp != null) {
            mReadCardHelp.cancelReadCard(null);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);
        mBinding.readId.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReadCardHelp != null){
                    mReadCardHelp.cancelReadCard(null);
                }
                mMisc.beep();
                mReadCardHelp.readCardNum(new ReturnValueCallback() {
                    @Override
                    public void run(ReturnValue result) {
                        if (result != null) {
                            Message message = Message.obtain();
                            message.what = 1;
                            String msg;
                            Object tag = result.getTag();
                            if (tag != null)
                                msg = tag.toString();
                            else
                                msg = "未知错误";
                            message.obj = msg;
                            mScaleHandler.sendMessage(message);
                        }
                    }
                });
            }
        });
        mBinding.readBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReadCardHelp != null){
                    mReadCardHelp.cancelReadCard(null);
                }
                mMisc.beep();
                mReadCardHelp.readCardId(new ReturnValueCallback() {
                    @Override
                    public void run(ReturnValue result) {
                        if (result != null) {
                            Message message = Message.obtain();
                            message.what = 0;
                            String msg;
                            Object tag = result.getTag();
                            if (tag != null)
                                msg = tag.toString();
                            else
                                msg = "未知错误";
                            message.obj = msg;
                            mScaleHandler.sendMessage(message);
                        }
                    }
                });
            }
        });
        mBinding.readProductBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReadCardHelp != null){
                    mReadCardHelp.cancelReadCard(null);
                }
                mMisc.beep();
                mReadCardHelp.readCardIdAndProduct(new ReturnValueCallback() {
                    @Override
                    public void run(ReturnValue result) {
                        if (result != null) {
                            Message message = Message.obtain();
                            message.what = 3;
                            String msg;
                            Object tag = result.getTag();
                            if (tag != null)
                                msg = tag.toString();
                            else
                                msg = "未知错误";
                            message.obj = msg;
                            mScaleHandler.sendMessage(message);
                        }
                    }
                });
            }
        });

        mBinding.writeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReadCardHelp != null){
                    mReadCardHelp.cancelReadCard(null);
                }
                if (TextUtils.isEmpty(mBinding.writeCard.getText().toString())) {
                    Toast.makeText(getActivity(), "请输入名字", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!mReadCardHelp.isChineseWord(mBinding.writeCard.getText().toString())) {
                    Toast.makeText(getActivity(), "请输入中文名字", Toast.LENGTH_SHORT).show();
                    return;
                }
                String cardIn = mBinding.writeCard.getText().toString();
                if (cardIn.length() <= 4) {
                    for (int i = cardIn.length(); i < 4; i++) {
                        cardIn += "姚";
                    }

                }

                String nameWriteStr = toUnicode(cardIn).replace("\\u", "");
                for (int i = 0; i < 16; i++) {
                    writeData[i] = (byte) nameWriteStr.charAt(i);
                }
                mMisc.beep();
                mReadCardHelp.writeCardBlock(new ReturnValueCallback() {
                    @Override
                    public void run(ReturnValue result) {
                        if (result != null) {
                            Message message = Message.obtain();
                            message.what = 0;
                            String msg;
                            Object tag = result.getTag();
                            if (tag != null)
                                msg = tag.toString();
                            else
                                msg = "未知错误";
                            message.obj = msg;
                            mScaleHandler.sendMessage(message);
                        }
                    }
                }, writeData);
            }
        });


        mBinding.writeProductBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mReadCardHelp != null){
                    mReadCardHelp.cancelReadCard(null);
                }
                if (TextUtils.isEmpty(mBinding.writeCardProduct.getText().toString())) {
                    Toast.makeText(getActivity(), "请输入名字", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!mReadCardHelp.isChineseWord(mBinding.writeCardProduct.getText().toString())) {
                    Toast.makeText(getActivity(), "请输入中文名字", Toast.LENGTH_SHORT).show();
                    return;
                }
                String cardIn = mBinding.writeCardProduct.getText().toString();
                if (cardIn.length() <= 4) {
                    for (int i = cardIn.length(); i < 4; i++) {
                        cardIn += "姚";
                    }

                }
                String nameWriteProductStr = toUnicode(cardIn).replace("\\u", "");
                for (int i = 0; i < 16; i++) {
                    writeDataProduct[i] = (byte) nameWriteProductStr.charAt(i);
                }
                mMisc.beep();
                mReadCardHelp.writeCardProductBlock(new ReturnValueCallback() {
                    @Override
                    public void run(ReturnValue result) {
                        if (result != null) {
                            Message message = Message.obtain();
                            message.what = 0;
                            String msg;
                            Object tag = result.getTag();
                            if (tag != null)
                                msg = tag.toString();
                            else
                                msg = "未知错误";
                            message.obj = msg;
                            mScaleHandler.sendMessage(message);
                        }
                    }
                }, writeDataProduct);




            }
        });


        return root;

    }


}
